import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
import qs from 'qs'

const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
const baseUrlSchema = CommonConfig.schemaUrl + '/v1'
const baseUrlWms = CommonConfig.wms + '/v1'

const repairUrl = CommonConfig.tpmUrl + '/v1/device_repair'
const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

/**
 * 列举工单
 * @param params
 * @param listType 1维修申请，2维修工单，3维修记录
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getDeviceRepairList (params, listType) {
  const newUrl = `${repairUrl}/list`
  params['listType'] = listType
  let reqData = {
    params,
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 单个删除
export function deviceRepair (id) {
  const newUrl = `${repairUrl}/delete/${id}`

  return axiosInstance.delete(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 批量删除
export function deviceRepairIds (ids) {
  const newUrl = `${repairUrl}/delete`
  let reqData = {
    params: {
      ids
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.delete(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 获取维修详情
function getRepairDetail (id) {
  const newUrl = `${repairUrl}/detail/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
export function point (userId, ids) {
  console.log(ids, 'ids')
  const newUrl = `${repairUrl}/send/${userId}`
  let reqData = ids
  return axiosInstance.put(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 模糊搜索设备下拉
function getDeviceList (params) {
  const reqData = {
    params: {
      ...params,
      pageNum: 1,
      pageSize: 10000
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  const newUrl = `${baseUrlTpm}/devices/select/bySchemeAndDevice`
  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 获取解决方案
function getSolution (id) {
  const newUrl = `${repairUrl}/detail/item/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

function createDeviceRepair (params) {
  params['isPc'] = 1
  const newUrl = repairUrl + '/save'
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
function editDeviceRepair (params) {
  params['isPc'] = 1 // 后台要求标注pc
  const newUrl = repairUrl + '/update'
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取审核人
 * @param isOutsourc 是否为委外
 * @returns {Promise<unknown>}
 */
function getApproval (isOutsourc = true) {
  const params = {
    flowCode: isOutsourc ? 2 : 1
  }
  const newUrl = `${repairUrl}/approver`

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res.data))
  })
}

// 设置审核人
function setUpApprover (params) {
  const newUrl = `${repairUrl}/approver?operation=1`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 接单
function receiving (id) {
  const newUrl = `${repairUrl}/receiving/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// 维修开始
function beginRepair (id) {
  const newUrl = `${repairUrl}/to_repair/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 暂停/取消暂停
 * @param id 维修单id
 * @param isSuspend 暂停操作
 * @returns {Promise<AxiosResponse<any>>}
 */
function suspendRepair (id, isSuspend = true, data) {
  let newUrl = `${repairUrl}/cancel_suspend/${id}`
  isSuspend && (newUrl = `${repairUrl}/suspend/${id}`)
  const params = {
    cost: data.cost,
    describe: data.remark,
    photoArray: data.imgList
  }
  return axiosInstance.put(newUrl, isSuspend ? params : {}).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 待维修的维修单打回
 * @param id 维修单id
 * @returns {Promise<AxiosResponse<any>>}
 */
function repulseNoRepair (id) {
  let newUrl = `${repairUrl}/no_repair:back/${id}`

  return axiosInstance.put(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
/**
 * 维修中的维修单打回
 * @returns {Promise<AxiosResponse<any>>}
 */
function repulseRepair (params) {
  let newUrl = `${repairUrl}/repaired:back`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}
// 委外方式枚举
function getWYType () {
  let newUrl = `${repairUrl}/out_side/type`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res.data))
  })
}
// 申请委外
function addWYApply (params) {
  let newUrl = `${repairUrl}/out_side`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

/**
 * 获取数据字典
 * @param dicGroupCode repair_wy_reason/repair_wy_company  维修委外原因/维修委外单位
 * @param disable 是否禁用
 * @returns {Promise<unknown>}
 */
function getDict (dicGroupCode = 'repair_wy_reason', disable = true) {
  const newUrl = baseUrlSchema + '/dict_by_gcode'
  let params = {
    dicGroupCode,
    disable
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}
// orderType 1：维修工单 2：保养工单
function getIsOut (orderId, orderType) {
  const newUrl = baseUrlWms + '/spare_part_use_apply/is_out'
  let params = {
    orderId,
    orderType
  }
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 维修结束
function finishRepair (params) {
  let newUrl = `${repairUrl}/finish`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// 验收
function confirmRepair (id, params) {
  let newUrl = `${repairUrl}/confirm/${id}`

  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// 导出维修履历
export function exportRepair (params) {
  let newUrl = `${repairUrl}/repair_report/excel:export`

  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

// 保存维修工单描述和记录
function submitDescriptionRecord (params) {
  let newUrl = `${repairUrl}/description_record`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve((res))
  })
}

export default {
  getRepairDetail,
  getDeviceList,
  getSolution,
  createDeviceRepair,
  editDeviceRepair,
  getApproval,
  setUpApprover,
  receiving,
  beginRepair,
  suspendRepair,
  repulseNoRepair,
  repulseRepair,
  getWYType,
  addWYApply,
  getDict,
  getIsOut,
  finishRepair,
  confirmRepair,
  submitDescriptionRecord
}
